/**
 * @author hsy541
 */

var treeData = [ {
	id : 'selectedSampleTreeNode',
	text : 'Selected samples',
	expandable : true,
	leaf : false,
	children : []
}, {
	text : 'Graph data',
	expandable : true,
	leaf : false,
	children : []
}, {
	text : 'Output data',
	expandable : true,
	leaf : false,
	children : []
} ];

var nodeDbl = function(node) {
	if (centerPanel.getItem(node.text))
		centerPanel.activate(centerPanel.getItem(node.text));
	else
		centerPanel.add(new Ext.Panel( {
			id : node.text,
			bodyStyle : 'padding:15px',
			title : node.text.split(" ")[0],
			// iconCls : 'tabs',
			closable : true,
			autoLoad : {
				url : 'TableBrowser/fetchSampleDetail',
				scripts : true,
				params : {
					sampleID : node.text.split(" ")[0]
				}
			}
		})).show();
	// Ext.getCmp('studyInfo').getForm().setValues( [ {
	// id : 'studyID',
	// value : 'SRS0343'
	// } ]);

}

var showGroup = function(btn, obj, data) {
}

var westTreePanel = new Ext.tree.TreePanel( {
	// xtype: 'panel' implied by default
	title : 'Data Tree',
	region : 'west',
	xtype : 'treepanel',
	useArrows : true,
	autoScroll : true,
	animate : true,
	containerScroll : true,
	split : true,
	enableDD : false,
	loader : new Ext.tree.TreeLoader( {
		preloadChildren : true
	}),
	root : new Ext.tree.AsyncTreeNode( {
		expanded : true,
		children : treeData
	}),
	rootVisible : false,
	// listeners : {},

	margins : '5 0 0 5',
	width : 200,
	collapsible : true, // make collapsible
	// cmargins: '5 5 0 5', // adjust top margin when collapsed
	id : 'west-region-container',
	listeners : {
		'dblclick' : nodeDbl
	},
	tbar : new Ext.Toolbar( {
		items : [ {
			// xtype: 'button', // default for Toolbars, same as 'tbbutton'
			text : 'Show Group',
			listeners : {
				'click' : function() {
					var msg = '', selNodes = westTreePanel.getChecked();
					Ext.each(selNodes, function(node) {
						if (msg.length > 0) {
							msg += ',';
						}
						msg += node.text.split(" ")[0];
					});
					if (centerPanel.getItem(msg))
						centerPanel.activate(centerPanel.getItem(msg));
					else
						centerPanel.add(new Ext.Panel( {
							id : msg,
							bodyStyle : 'padding:15px',
							title : 'Group',
							// iconCls : 'tabs',
							closable : true,
							autoLoad : {
								url : 'TableBrowser/showGroup',
								scripts : true,
								params : {
									sampleIDs : msg
								}
							}
						})).show();
				}
			}
		} ]
	})

});